package com.dns.reset.web.service;

import com.dns.reset.web.model.DbTelnetEntity;
import com.dns.reset.web.repository.DbTelnetRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class DbTelnetService {
    protected static Logger logger = LoggerFactory.getLogger(DbTelnetService.class);
    @Autowired
    private DbTelnetRepository dbTelnetRepository;

    public List<DbTelnetEntity> findAll(){
        return dbTelnetRepository.findAll();
    }
    public List<DbTelnetEntity> findAllOrderByRuleIdDesc(){
        Sort sort = new Sort(Sort.Direction.DESC, "ruleId");
        return dbTelnetRepository.findAll(sort);
    }

    public  void  save(DbTelnetEntity dbTelnetEntity){
        if(StringUtils.isNotBlank(dbTelnetEntity.getIp())) {
            if (dbTelnetEntity.getRuleId() == null || dbTelnetEntity.getRuleId() == 0) {
                synchronized (this) {
                    Integer ruleId = minRuleId();
                    dbTelnetEntity.setRuleId(ruleId);
                    String telnetCmd = "rule " + ruleId + " permit ip destination " + dbTelnetEntity.getIp() + " 0";
                    dbTelnetEntity.setTelnetCommand(telnetCmd);
                    dbTelnetRepository.save(dbTelnetEntity);
                }
            }else {
                dbTelnetRepository.save(dbTelnetEntity);
            }
        }else{
            logger.error("ip地址为空值");
        }
    }
    public List<DbTelnetEntity> findByIsExist(Integer isExist){
        return dbTelnetRepository.findByIsExist(isExist);
    }
    public Integer minRuleId(){
        int result =1;
        List<DbTelnetEntity> dbTelnetEntities=findAllOrderByRuleIdDesc();
        List<Integer> a=new ArrayList<Integer>();
        List<Integer> b=new ArrayList<Integer>();
        if(dbTelnetEntities.size()>0){
            for(int i=1;i<=dbTelnetEntities.get(0).getRuleId();i++){
                a.add(i);
            }
            for (DbTelnetEntity dbTelnetEntity:dbTelnetEntities){
                b.add(dbTelnetEntity.getRuleId());
            }
            a.removeAll(b);
            List<Integer> c=new ArrayList<Integer>();
            Iterator<Integer> it = a.iterator();
            while (it.hasNext()) {
                c.add(it.next());
            }
            if(c.size()==0){
                result=dbTelnetEntities.get(0).getRuleId()+1;
            }else{
                result=c.get(0);
            }
        }
        return  result;
    }

    public Page<DbTelnetEntity> queryForPage(int page , int size) {
        Sort sort = new Sort(Sort.Direction.ASC, "ruleId");
        Pageable pageable = new PageRequest(page-1, size, sort);
        return dbTelnetRepository.findAll(pageable);
    }
}
